<template>
<div>
  <Card :bordered="false" dis-hover>
    <div class="behind-wrapper clearfix">
      <div class="content-wrapper">
        <div class="button-group">
          <a @click='save' :disabled="saveDisabled"><i class="iconfont icon-head-save"></i>保存</a>
          <a @click='publish'><i class="iconfont icon-head-release"></i>发布</a>
          <a v-show="false"><i class="iconfont icon-head-add"></i>新增</a>
          <a v-show="false"><i class="iconfont icon-dump"></i>删除</a>
        </div>
        <div class="page-content">
          <div class="block-content">
            <h2 class="behind-title"><i class="iconfont icon-cm-info"></i>基本资料</h2>
            <div class="c-line">
              <div class="w-50">
                <div class="text-input displayflex require">
                  <span class="flexitem">新闻标题</span>
                  <div class="flex1">
                    <input type="text" v-model="detail.Title" placeholder="" :disabled='disabled' maxlength="20">
                  </div>
                </div>
              </div>
              <div class="w-50">
                <p class="red-tips">注：最多可输入20个字</p>
              </div>
            </div>
            <div class="c-line">
              <div class="w-50">
                <div class="text-input displayflex require">
                  <span class="flexitem">新闻摘要</span>
                  <div class="flex1">
                    <textarea v-model="detail.Summary" placeholder="" :disabled='disabled' cols="30" rows="3" maxlength="150"></textarea>
                  </div>
                </div>
              </div>
              <div class="w-50">
                <p class="red-tips">注：最多可输入150个字</p>
              </div>
            </div>
            <div class="c-line">
              <div class="clearfix" style="margin: 0 8px;">
                <editor @editValue="addParagraph" :disabled='disabled'></editor>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </Card>
</div>
</template>

<script>
export default {
  data() {
    return {
      // 新闻信息发布数据绑定
      detail: {},
      disabled: false,
      loading: true,
      IsPublish: '',
      saveDisabled: false,
    }
  },
  mounted() {
    this.detail.index = 2;
    this.detail.Content = this.detail.Content;
    this.$bus.emit('showEditorModel', this.detail);
  },
  methods: {
    getData() {
      var self = this;

      this.axios.get('/api/news/details/' + this.$route.params.guid)
        .then(function(res) {
          self.IsPublish = res.IsPublish;
          self.detail = res;
          self.detail.index = 2;
          self.$bus.emit('showEditorModel', self.detail);
        })
    },
    save() {
      var self = this;

      if (!this.detail.Title) {
        this.$Message.error('请填写新闻标题!');
        return;
      } else if (!this.detail.Summary) {
        this.$Message.error('请填写新闻摘要!');
        return;
      }

      if (this.$route.params.falg == "false") {
        this.detail.GUID = this.$route.params.guid;
      } else {
        this.detail.GUID = "";
      }

      this.saveDisabled = true;
      this.$store.commit('loading');

      this.axios.post('/api/news/save', this.detail).then(function(res) {
        self.$store.commit('loading', false);

        if (!res.message) {
          self.$Message.success('保存成功');
          if (res.guid) {
            self.$route.params.guid = res.guid;
          }

          self.$router.push('/behind/newsEditList')
        }
      }).catch(function(res) {
        self.saveDisabled = true;
        console.log(JSON.stringify(res));
        self.$store.commit('loading', false);
        self.$Message.error("保存失败");
      });
    },
    publish: function() {
      var self = this;

      if (this.$route.params.falg == "false") {
        this.detail.GUID = this.$route.params.guid;
      } else {
        this.detail.GUID = "";
      }
      if (this.$route.params.guid == "00") {
        this.$Message.error("请先保存再发布");
        return;
      }
      this.axios
        .post("/api/news/publish", {
          GUID: this.$route.params.guid
        })
        .then(function(res) {
          if (!res.message) {
            self.$Message.success("发布成功");
          }

          self.$router.push('/behind/newsEditList')
        });
    },
    addParagraph(editValue) {
      this.detail.Content = editValue;
    }
  },
  created() {
    if (this.$route.params.falg == 'false') {
      this.getData();
    }
  },
  activated() {
    if (this.$route.params.falg == 'false') {
      this.getData();
    }
  },
  watch: {
    detail: {
      handler: function(val, oldVal) {
        self.IsPublish = 'N';
      },
      deep: true
    },
  }
}
</script>


